package com.h2cloud;

/**
 * @Package: com.h2cloud
 * @ClassName: EmailValidator
 * @Author: wulongkai
 * @CreateTime: 2024/8/16 15:30
 * @Description:
 */
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class EmailValidator {
    // 定义邮箱的正则表达式
    private static final String EMAIL_PATTERN = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";

    // 创建Pattern对象
    private static final Pattern pattern = Pattern.compile(EMAIL_PATTERN);

    // 方法来验证邮箱地址
    public static boolean isValidEmail(String email) {
        if (email == null) {
            return false;
        }
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

    public static void main(String[] args) {
        // 测试示例
        String[] testEmails = {
                "test@example.com",
                "invalid-email",
                "another.test@domain.co",
                "user@subdomain.domain.com",
                "user@domain",
                "@domain.com",
                "user@domain..com"
        };

        for (String email : testEmails) {
            System.out.println(email + " is valid: " + isValidEmail(email));
        }
    }
}

